﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

namespace DuocThaiBinh.Common
{
    public class Util
    {
        public static decimal ConvertCurrencyToMoney(string money)
        {
            if (string.IsNullOrEmpty(money))
            {
                money = "0";
            }
            if (money.Contains("("))
            {
                money = money.Replace("(", "-").Replace(")", "");
            }
            return (Convert.ToDecimal(money.Replace(",", "")));
        }

        public static string ConvertDecimaToCurrency(decimal money)
        {
            return string.Format("{0:C}",money).Replace("$","");
        }
        public static int ConvertObjectToIn(object input)
        {
            int result=0;            
            if (input != null)
            {
                if (Int32.TryParse(Convert.ToString(input),NumberStyles.Integer,CultureInfo.InvariantCulture,out result))
                {
                    return result;
                }
            }
            else result = 0;
            return result;
        }
        public static decimal ConvertObjectToDecima(object input)
        {
            decimal result = 0;
            if (input != null)
            {
                if (Decimal.TryParse(Convert.ToString(input), out result))
                {
                    return result;
                }
            }
            else result = 0;
            return result;
        }

        public static DateTime ConvertObjectToDateTime(Object input)
        {
            string strInput =string.Format("{0:dd/MM/yyyy}",input);
            return DateTime.ParseExact(strInput, "dd/MM/yyyy", null);
        }
        public static string ConvertObjectToStringDateTime(Object input)
        {
            string strInput = string.Format("{0:dd/MM/yyyy}", input);
            return DateTime.ParseExact(strInput, "dd/MM/yyyy", null).ToString("dd/MM/yyyy");
        }

        public static void Log(string msg)
        {
            try
            {
                string logPath = System.Configuration.ConfigurationManager.AppSettings["LogPath"];
                logPath = HttpContext.Current.Server.MapPath(logPath);
                logPath = System.IO.Path.Combine(logPath, string.Format("Log_{0}.txt", DateTime.Today.ToString("yyyyMMdd")));
                if (!System.IO.File.Exists(logPath))
                {
                    if (!System.IO.Directory.Exists(logPath))
                    {
                        System.IO.Directory.CreateDirectory(logPath);
                    }
                    
                    System.IO.File.WriteAllText(logPath, msg + Environment.NewLine);
                }
                else
                {
                    using (var writer = System.IO.File.AppendText(logPath))
                    {
                        writer.Write(msg + Environment.NewLine);
                        writer.Flush();
                    }
                }
            }
            catch
            {
            }
        }

        public static void Log(System.Exception ex)
        {
            Log(ex.Message + Environment.NewLine + ex.StackTrace);
        }
    }
}